//当前播放的音乐信息，当前audio的状态
const audioInfo = {
  state:{
    audioEle:'',
    curIndex:0,
    musicInfo:{
      id:-1,
      name:'',
      singer:'',
      img_url:'',
      url:'',
      type:'',
      lyric:'',
    },
    isPlaying:false,
    //播放类型 ：0.顺序播放 ， 1.顺序循环 ， 2.随机播放 ， 3.单曲循环
    playType:0,
    playedTime:0,
    duration:0,
  },
  getters:{
    getAudioEle: state => state.audioEle,
    getPlay: state => state.isPlaying,
    getCurMusic: state => state.musicInfo,
  },
  mutations:{
    setAudioEle(state,ele){
      state.audioEle = ele;
    },
    play(state){
      state.isPlaying = true;
      state.audioEle.play();
    },
    pause(state){
      state.isPlaying = false;
      state.audioEle.pause();
    },
    togglePlay(state){
      let audio = state.audioEle;
      state.isPlaying = ! state.isPlaying;
      if(audio){
        if(state.isPlaying)
          audio.play();
        else
          audio.pause();
      }
    },
    setMusic(state,obj){
      state.musicInfo = obj;
    }
  },
  actions:{
    set_AudioEle({commit},ele){
      commit('setAudioEle',ele);
    },
    set_Music({commit},obj){
      commit('setMusic',obj);
    },
    updateCurmusic({state,commit,rootState},id){
      let res = false,
          list = rootState.sheetInfo.musicList;

      for(let i=0;i<list.length;i++){
        if(list[i].id == id){
          commit('setMusic',list[i]);
          res = true;
        }
      }
      if(!res) console.warn(`没有查到ID 为${id} 的music.`);
    },
    set_curMusic({commit},obj){
      commit('setMusic',obj);
    },
    set_play({commit,rootState}){
      setTimeout(function(){
        commit('play');
      },20)
    }
  }
}

export default audioInfo;
